package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_UNION_WAR_ACTIVITY_GET_CHAMPION_RANK_LIST, desc = "获取历届冠军榜单")
public class UnionWarActivityGetChampionRanKListCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        CrossUnionWarProto.UnionActivityChampionRankListRespMsg.Builder respMsg = CrossUnionWarProto.UnionActivityChampionRankListRespMsg.newBuilder();
        UnionActivityRankProto.UnionActivityRankListMsg.Builder listBuilder = UnionWarActivityMgr.getChampionRankBuilder();
        String unionUid = player.getUserInfo().getUnionUid();
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        if(listBuilder != null){
            respMsg.setRankList(listBuilder);
            if(!StringUtils.isNullOrEmpty(unionUid)){
                long serverId = GameServer.getInstance().getServerId();
                int rank = 1;
                for(UnionActivityRankProto.UnionActivityRankTempMsg rankTempMsg : listBuilder.getRankListList()) {
                    if(rankTempMsg.getUnionUid().equals(unionUid) && rankTempMsg.getUnionBaseData().getServerId() == serverId){
                        respMsg.setValue(rankTempMsg.getValue());
                        respMsg.setMyRank(rank);
                        break;
                    }
                    rank++;
                }
            }
        }else {
            respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());
        }
        player.sendPacket(Protocol.U_UNION_WAR_ACTIVITY_GET_CHAMPION_RANK_LIST, respMsg);
    }
}
